package com.chuan.it_market.service.impl;

import com.chuan.it_market.dao.BuyerDao;
import com.chuan.it_market.dao.GoodsDao;
import com.chuan.it_market.dao.SellerDao;
import com.chuan.it_market.dao.UserOrderDao;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Buyer;
import com.chuan.it_market.pojo.entity.Goods;
import com.chuan.it_market.pojo.entity.Seller;
import com.chuan.it_market.pojo.entity.UserOrder;
import com.chuan.it_market.pojo.vo.UserOrderVO;
import com.chuan.it_market.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    UserOrderDao userOrderDao;
    @Resource
    GoodsDao goodsDao;
    @Resource
    SellerDao sellerDao;
    @Resource
    BuyerDao buyerDao;
    UserOrder userOrder=new UserOrder();

    @Override
    public int insert(UserOrder userOrder) {
        return userOrderDao.insertSelective(userOrder);
    }

    @Override
    public List<UserOrderVO> orderList(Integer user) {

                List<UserOrderVO> userOrderVOList= userOrderDao.selectByBno(user);
               for ( UserOrderVO u:userOrderVOList){
                   Goods goods= goodsDao.selectByPrimaryKey(u.getGno());
                   Seller seller=sellerDao.selectByPrimaryKey(u.getSno());
                   Buyer buyer=buyerDao.selectByPrimaryKey(u.getBno());
                  u.setSeller(seller);
                  u.setGoods(goods);
                  u.setBuyer(buyer);
               }
               return userOrderVOList;
    }

    @Override
    public int receive(Integer ono) {
        userOrder.setOno(ono);
        userOrder.setStatus("已收货");
        return userOrderDao.updateByPrimaryKeySelective(userOrder) ;
    }

    @Override
    public int back(Integer ono) {
        userOrder.setOno(ono);
        userOrder.setStatus("退货中");
        return userOrderDao.updateByPrimaryKeySelective(userOrder) ;
    }

    //买家删除 tag值为0 则改为1 tag值为2 则改为3
    @Override
    public int delete1(Integer ono) {
        //根据ono查出订单 为了获取订单tag的值
        UserOrder userOrder=userOrderDao.selectByPrimaryKey(ono);
        String tag=userOrder.getTag();
        if (tag.equals("0")){
            userOrder.setTag("1");
        }
        else userOrder.setTag("3");
        return userOrderDao.updateByPrimaryKeySelective(userOrder);
    }

    //卖家删除 tag值为0 则改为1 tag值为2 则改为3
    @Override
    public int delete2(Integer ono) {
        //根据ono查出订单 为了获取订单tag的值
        UserOrder userOrder=userOrderDao.selectByPrimaryKey(ono);
        String tag=userOrder.getTag();
        if (tag.equals("0")){
            userOrder.setTag("2");
        }
        else userOrder.setTag("3");
        return userOrderDao.updateByPrimaryKeySelective(userOrder);
    }

    @Override
    public List<UserOrderVO> selectOrderByStatus(String status, Integer bno) {
        List<UserOrderVO> userOrderVOList= userOrderDao.selectOrderByStatus(status,bno);
        for ( UserOrderVO u:userOrderVOList){
            Goods goods= goodsDao.selectByPrimaryKey(u.getGno());
            Seller seller=sellerDao.selectByPrimaryKey(u.getSno());
            u.setSeller(seller);
            u.setGoods(goods);
        }
        return userOrderVOList;
    }

    @Override
    public int receive1(Integer ono) {
        userOrder.setOno(ono);
        userOrder.setStatus("运输中");
        return userOrderDao.updateByPrimaryKeySelective(userOrder) ;
    }

    @Override
    public int back1(Integer ono) {
        userOrder.setOno(ono);
        userOrder.setStatus("退款成功");
        return userOrderDao.updateByPrimaryKeySelective(userOrder) ;
    }

    @Override
    public int back2(Integer ono) {
        userOrder.setOno(ono);
        userOrder.setStatus("运输中");
        return userOrderDao.updateByPrimaryKeySelective(userOrder) ;
    }

    @Override
    public List<UserOrderVO> orderList2(Integer userId) {
        List<UserOrderVO> userOrderVOList= userOrderDao.selectBySno(userId);
        for ( UserOrderVO u:userOrderVOList){
            Goods goods= goodsDao.selectByPrimaryKey(u.getGno());
            Seller seller=sellerDao.selectByPrimaryKey(u.getSno());
            Buyer buyer=buyerDao.selectByPrimaryKey(u.getBno());
            u.setSeller(seller);
            u.setGoods(goods);
            u.setBuyer(buyer);
        }
        return userOrderVOList;
    }

    @Override
    public ResponseDTO Statistics(Integer userId) {
        List<UserOrderVO> userOrderVOList= userOrderDao.selectBySno(userId);
        for ( UserOrderVO u:userOrderVOList){
            Goods goods= goodsDao.selectByPrimaryKey(u.getGno());
            Seller seller=sellerDao.selectByPrimaryKey(u.getSno());
            Buyer buyer=buyerDao.selectByPrimaryKey(u.getBno());
            u.setSeller(seller);
            u.setGoods(goods);
            u.setBuyer(buyer);
        }
        return ResponseDTO.ok("查询成功",userOrderVOList);
    }


}
